package com.GreenIce.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.GreenIce.android.UI.UImanager;
import com.GreenIce.android.datastructure.ConfigData;
import com.GreenIce.android.datastructure.ConfigHandler;
import com.GreenIce.android.util.DirectionIdentifier;
import com.GreenIce.android.util.DistanceSensor;
import com.GreenIce.android.util.HeadSetManager;

public class GreenIceActivity extends Activity {
    /** Called when the activity is first created. */
	SensorManager mSensorManager;

    PowerManager.WakeLock wl;
	UImanager uimanager; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;        
        mSensorManager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
        DirectionIdentifier.getInstance().register(mSensorManager);
        DistanceSensor.getInstance().register(mSensorManager);
        
        
        uimanager = UImanager.getInstance();
        uimanager.setMainView();
        
        ConfigHandler.initData();
        ConfigHandler.loadData();
        
        PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SCREEN_DIM_WAKE_LOCK");
        wl.acquire();
        
        registerReceiver(HeadSetManager.getInstance(), new IntentFilter(Intent.ACTION_HEADSET_PLUG));
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
    @Override
    protected void onResume() {
    	wl.acquire();
    	DirectionIdentifier.getInstance().register(mSensorManager);
    	DistanceSensor.getInstance().register(mSensorManager);
    	
    	activity = this;
    	UImanager.getInstance().setActivity(this);
    	ConfigHandler.loadData();
    	if(ConfigData.getInstance().status == 0){
    		UImanager.getInstance().setMainView();
    	}
    	else if(ConfigData.getInstance().status == 1){
    		UImanager.getInstance().setChooseWorldView();
    	}
    	else if(ConfigData.getInstance().status == 2){
    		UImanager.getInstance().setChooseLevelView(ConfigData.getInstance().curWorld);
    	}
    	else if(ConfigData.getInstance().status == 3){
    		UImanager.getInstance().setGameStartView(ConfigData.getInstance().curLevel);
    	}
    	else if(ConfigData.getInstance().status == 5){
    		UImanager.getInstance().setGameOverView(ConfigData.getInstance().curScore, 
    												ConfigData.getInstance().curStar);
    	}
    	registerReceiver(HeadSetManager.getInstance(), new IntentFilter(Intent.ACTION_HEADSET_PLUG));
    	super.onResume();
    }
    @Override
    protected void onPause() {
    	wl.release();
    	DirectionIdentifier.getInstance().unregister(mSensorManager);
    	DistanceSensor.getInstance().unregister(mSensorManager);
    	ConfigHandler.saveData();
    	unregisterReceiver(HeadSetManager.getInstance());
    	super.onPause();
    }
    static Activity activity;
    public static Activity getActivity(){
    	return activity;
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(keyCode == KeyEvent.KEYCODE_BACK){
    		if(ConfigData.getInstance().status == 0){
    			Dialog dialog = new AlertDialog.Builder(this)
				  .setTitle(R.string.exit)
				  .setMessage(R.string.exit_info)
				  .setPositiveButton(R.string.yes,
				  new DialogInterface.OnClickListener() 
				  {
					public void onClick(DialogInterface dialog, int whichButton){
						finish();
					}
				  })
				  .setNegativeButton(R.string.no, null)
				  .create();
				dialog.show();
				return true;
    		}
    		else if(ConfigData.getInstance().status == 1){
    			UImanager.getInstance().setMainView();
    			return true;
    		}
    		else if(ConfigData.getInstance().status == 2){
    			UImanager.getInstance().setChooseWorldView();
    			return true;
    		}
    		else if(ConfigData.getInstance().status == 3){
    			UImanager.getInstance().setChooseLevelView(ConfigData.getInstance().curWorld);
    			return true;
    		}
    		else if(ConfigData.getInstance().status == 4){//do nothing
    			return true;
    		}
    		else if(ConfigData.getInstance().status == 5){
    			UImanager.getInstance().setChooseLevelView(ConfigData.getInstance().curWorld);
    			return true;
    		}
    	}
    	return super.onKeyDown(keyCode, event);
    }
    
    protected void onDestory() {
    	super.onDestroy();
    	unregisterReceiver(HeadSetManager.getInstance());
	}
}